package com.shakespace.artofandroid.chapter05remoteview

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.widget.RemoteViews
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import com.shakespace.artofandroid.MainActivity
import com.shakespace.artofandroid.R
import kotlinx.android.synthetic.main.activity_remote_view.*

class RemoteViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_remote_view)

        /**
         * 获取notificationManager
         * 1. Android O 以上， 创建NotificationChannel
         * 2、通过NotificationCompat.Builder 创建 一条通知
         * 3、发送notificationManager.notify(0, notification)
         *
         * 4、需要响应点击事件，需要创建intent， setContentIntent（另外还有两个类似方法）
         * 5、可以通过setView 自定义布局， 布局是一个RemoteViews
         *
         */
        notification.setOnClickListener {
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                /**
                 * * IMPORTANCE_NONE 关闭通知
                 * IMPORTANCE_MIN 开启通知，不会弹出，但没有提示音，状态栏中无显示
                 * IMPORTANCE_LOW 开启通知，不会弹出，不发出提示音，状态栏中显示
                 * IMPORTANCE_DEFAULT 开启通知，不会弹出，发出提示音，状态栏中显示
                 * IMPORTANCE_HIGH 开启通知，会弹出，发出提示音，状态栏中显示
                 */
                val channel =
                    NotificationChannel("artofandroid", "aoa", NotificationManager.IMPORTANCE_HIGH)
//                channel.setAllowBubbles(true)
//                channel.setBypassDnd(true)
//                channel.enableLights(true)
//                channel.lightColor = R.color.green
                //  will show number on you app icon
//                channel.setShowBadge(true)
//                channel.lockscreenVisibility = 1000
                //  if you have set IMPORTANCE_HIGH , this will not working
                // if you have set pendingIntent+ IMPORTANT_DEFAULT, not working
//                channel.enableVibration(false)
//                channel.shouldVibrate()
                notificationManager.createNotificationChannel(channel)
            }

            val pendingIntent = PendingIntent.getActivity(
                this,
                0,
                Intent(this, MainActivity::class.java).also {
                    it.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                },
                0
            )

            val remoteview = RemoteViews(packageName, R.layout.layout_notification)

            remoteview.setTextViewText(R.id.nf_text, "remote view  title")
            remoteview.setImageViewResource(R.id.nf_image, R.drawable.material_ic_calendar_black_24dp)
            // 添加事件
            remoteview.setOnClickPendingIntent(R.id.nf_text, pendingIntent)

            /**
             * 在Android 8以上， 这里的channelId需要和 NotificationChannel 的第一个参数相同，否则通知不会显示
             */
            val notification = NotificationCompat.Builder(this, "artofandroid")
                //  xml(vector) is not acceptable here , only png or jpg
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.material_ic_calendar_black_24dp))
                .setSmallIcon(android.R.drawable.stat_notify_chat)  //  saw in status bar
                .setContentTitle("A new message")
                .setContentText("welcome to Macao gamble system")
                .setWhen(System.currentTimeMillis())

                .setLights(Color.GREEN, 1000, 1000)
//                .setVibrate(longArrayOf(100,200,300,400))
//                .setSound(Uri.fromFile(File("/system/media/audio/ringtones/Luna.ogg")))
                // set custom view
//                .setCustomContentView(remoteview)
//                .setContent()
                // if not setIntent ,autoCancel not work
                .setAutoCancel(true)
                // when click will trigger
                .setContentIntent(pendingIntent)
                // also can be trigger when  user clear notification
//                .setDeleteIntent(pendingIntent)
                // in some cases , will trigger intent directly ,such as when you in a call or in a game
                //  may need add permission SYSTEM_ALERT_WiNDOW
//                .setFullScreenIntent(pendingIntent,true)

                .build()

            /**
             *  Remember to save the notification ID that you pass to NotificationManagerCompat.notify()
             *  because you'll need it later if you want to update or remove the notification.
             */
            notificationManager.notify(0, notification)
        }
    }
}
